<?php
namespace ESTool;
/**
 * 统计es 健康状态
 * 
 * @author xainghui
 *         @date 2017-04-18
 */
class Health
{

    static $HOST;

    static $hostList = array(
        'local' => "http://127.0.0.1:9200",
        '61' => "http://127.0.0.1:9200", // "http://10.204.13.61:9200",
        '112' => "http://127.0.0.1:9200" // "http://10.204.13.113:9200",
    );

    /**
     * 查看服务器状态
     * 
     * @return number|unknown
     */
    static public function stat($host)
    {
        self::$HOST = self::$hostList[$host];
        return array_merge(self::site(), self::node(), self::index(), self::field());
    }

    static public function site()
    {
        // 概况
        $tmp = json_decode(Elasticsearchpool::Curl(self::$HOST . '/_cat/allocation?v&format=json'), true);
        $total['head'] = json_encode(array(
            array_keys(current($tmp))
        ));
        foreach ($tmp as $item) {
            $total['body'][] = array_values($item);
        }
        $total['body'] = json_encode($total['body']);
        return array(
            'total' => array(
                "title" => "概况",
                "data" => $total
            )
        );
    }

    static public function index()
    {
        // 索引数据
        $tmp = json_decode(Elasticsearchpool::Curl(self::$HOST . '/_cat/indices?format=json&pretty'), true);
        $index['head'] = json_encode(array(
            array_keys(current($tmp))
        ));
        foreach ($tmp as $item) {
            $color = $item['health'];
            foreach ($item as $k => $_i) {
                $item[$k] = '<font color="' . $color . '">' . $_i . '</font>';
            }
            $index['body'][] = array_values($item);
        }
        $index['body'] = json_encode($index['body']);
        $pri = $all = $doc = 0;
        foreach ($tmp as $item) {
            $pri += Elasticsearchpool::getSize($item['pri.store.size']);
            $all += Elasticsearchpool::getSize($item['store.size']);
            $doc += Elasticsearchpool::getSize($item['docs.count']);
        }
        $index_total['head'] = json_encode(array(
            array(
                "单个副本占用硬盘空间",
                "所有副本占用硬盘空间",
                "文档总数"
            )
        ));
        $index_total['body'] = json_encode(array(
            array(
                Elasticsearchpool::convert($pri),
                Elasticsearchpool::convert($all),
                Elasticsearchpool::convert($doc)
            )
        ));
        
        return array(
            'index_total' => array(
                "title" => "索引数据-汇总",
                "data" => $index_total
            ),
            'index' => array(
                "title" => "索引数据",
                "data" => $index
            )
        );
    }

    static public function field()
    {
        $field = $field_total = array();
        // 字段数据
        $tmp = json_decode(Elasticsearchpool::Curl(self::$HOST . '/_cat/fielddata?format=json&pretty'), true);
        $field['head'] = json_encode(array(array_keys(current($tmp))));
        foreach ($tmp as $item) {
        $field['body'][] = array_values($item);
        }
        $field['body'] = json_encode($field['body']);
        $field_index_size = 0;
        foreach ($tmp as $item) {
        $field_index_size += Elasticsearchpool::getSize($item['size']);
        }
        $field_total['head'] = json_encode(array(array("字段索引占用空间")));
        $field_total['body'] = json_encode(array(array(Elasticsearchpool::convert($field_index_size))));
        
        return array(
            'field_total' => array(
                "title" => "字段数据-汇总",
                "data" => $field_total
            ),
            'field' => array(
                "title" => "字段数据",
                "data" => $field
            )
        );
    }

    static public function node()
    {
        // 数据节点数据
        $tmp = json_decode(Elasticsearchpool::Curl(self::$HOST . '/_nodes'), true);
        foreach ($tmp["nodes"] as $k => $item) {
            $item = array(
                'name' => $item["name"],
                'host' => $item["host"],
                'es-ver' => $item["version"],
                'total_indexing_buffer' => Elasticsearchpool::convert($item["total_indexing_buffer"]),
                'is_data' => isset($item["settings"]["node"]["data"]) ? $item["settings"]["node"]["data"] : 0,
                'is_master' => isset($item["settings"]["node"]["master"]) ? $item["settings"]["node"]["master"] : 0,
                'java-ver' => $item["jvm"]["version"],
                'java-mem' => Elasticsearchpool::convert($item["jvm"]["mem"]["heap_init_in_bytes"])
            );
            $server[$k] = $item;
        }
        $node['head'] = json_encode(array(
            array_keys(current($server))
        ));
        foreach ($server as $item) {
            $node['body'][] = array_values($item);
        }
        $node['body'] = json_encode($node['body']);
        return array(
            'node' => array(
                "title" => "节点数据",
                "data" => $node
            )
        );
    }

    /**
     * 合并索引
     * 
     * @param array $source
     *            源数据索引
     * @param string $dest
     *            目标索引
     */
    static public function reindex($source, $dest)
    {
        $query = array(
            "source" => array(
                "index" => $source
            ),
            "dest" => array(
                "index" => $dest,
                "version_type" => "internal"
            )
        );
        $success = Elasticsearchpool::Curl(self::$HOST . '/_reindex', json_encode($query));
        return $success;
    }

    /**
     * 合并数据分区
     * 
     * @param array $source
     *            源数据索引
     */
    static public function forcemerge($source)
    {
        $url = self::$HOST . '/' . implode(",", (array) $source) . "/_forcemerge?max_num_segments=1";
        $success = Elasticsearchpool::Curl($url, "{}");
        return $success;
    }
}
